
import 'dart:io';

import '../interface/fcl_bmf_interface.dart';

class FclBmfLocationManager {
  // 私有构造函数
  FclBmfLocationManager._privateConstructor();

  // 保存单例
  static final FclBmfLocationManager _instance = FclBmfLocationManager._privateConstructor();

  // 工厂构造函数返回单例
  factory FclBmfLocationManager() {
    return _instance;
  }

  late FclBmfInterface fbPlatform;
  /// 初始化定位
  /// 设置返沪函数，用于返回定位结果
  /// ak: T9SXuGFPEgLwjfXXsuZ5U59bDTjqevpR
  Future<String?> initLocation({required String ak}) async {
    try {
      String? errorTips;
      if (Platform.operatingSystem == "ohos") {
        errorTips = await initOhosLocation(ak: ak);
      } else {
        errorTips = '未实现的平台';
      }
      return errorTips;
    } catch (e) {
      print('fcbmf------>requestPermission error: $e');
      rethrow;
    }
  }

  /// 初始化Ohos定位
  Future<String?> initOhosLocation({required String ak}) async {
    String? errorTips;
    try {
      fbPlatform = FclBmfInterface();
      String? vv = await fbPlatform.getPlatformVersion();
      print('fcbmf------>vv: $vv');
      String? idStr = await fbPlatform.getAppIdentifier();
      print('fcbmf------>idStr: $idStr');

      // ['ohos.permission.APPROXIMATELY_LOCATION', 'ohos.permission.LOCATION']
      String? permission = await fbPlatform.requestPermission();
      String permitStr = '';
      if (permission != null) {
        if (permission.contains('ohos.permission.APPROXIMATELY_LOCATION')) {
          permitStr = '模糊定位未授权';
        }
        if (permission.contains('ohos.permission.LOCATION')) {
          if (permitStr.isNotEmpty) {
            permitStr += '、';
          }
          permitStr = '精准定位未授权';
        }
      }
      print('fcbmf------>requestPermission: $permitStr');
      if (permitStr.isNotEmpty) {
        errorTips = '请授权位置权限：设置 > 应用和元服务 > 具体应用 > 位置';
        return errorTips;
      }

      bool isSuc = await fbPlatform.setAK(ak);
      print('fcbmf------>setAKisSuc: $isSuc');
      if (!isSuc) {
        errorTips = '设置AK失败';
        return errorTips;
      }
      isSuc = await fbPlatform.setAgreePrivacy(true);
      print('fcbmf------>setAgreePrivacyisSuc: $isSuc');
      if (!isSuc) {
        errorTips = '设置同意隐私协议失败';
        return errorTips;
      }
      return null;
    } catch (e) {
      print('fcbmf------>initOhosLocation error: $e');
      rethrow;
    }
  }

  /// 开始定位
  Future<String> startLocation() async {
    try {
      String rst = '';
      if (Platform.operatingSystem == "ohos") {
        rst = await fbPlatform.startLocation();
      } else {
        rst = '未实现的平台';
      }
      print('fcbmf------>startLocationisSuc: $rst');
      return rst;
    } catch (e) {
      print('fcbmf------>startLocation error: $e');
      rethrow;
    }
  }

  /// 停止定位
  Future<void> stopLocation() async {
    try {
      if (Platform.operatingSystem == "ohos") {
        bool isSuc = await fbPlatform.stopLocation();
        print('fcbmf------>stopLocationisSuc: $isSuc');
      } else {
        print('fcbmf------>stopLocationisSuc: 未实现的平台');
      }
    } catch (e) {
      print('fcbmf------>stopLocation error: $e');
      rethrow;
    }
  }

}